package com.ztools.okretrofit;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;


import com.ztools.common.R;
import com.ztools.base.BaseDialogFragment;

import java.util.Timer;
import java.util.TimerTask;


/**
 * 登录的dialog
 */
public class LoadingDialogFragment extends BaseDialogFragment {
    private static final String PARAM_SHOW_MSG = "massage";
    ImageView mIvLoading;
    TextView mTvDesc;
    private String msg = "加载中...";

    private long statTime;
    private long currTime;

    private Animation animation;
    private Timer timer;
    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            currTime = System.currentTimeMillis();
            if ((currTime - statTime) > 9 * 1000 && (currTime - statTime) < 11 * 1000) {
                if (getDialog() != null) {
                    getDialog().setCanceledOnTouchOutside(true);
                }
            } else {
                dismiss();
            }
        }
    };

    public static LoadingDialogFragment newInstance(String msg) {
        LoadingDialogFragment fragment = new LoadingDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PARAM_SHOW_MSG, msg);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static LoadingDialogFragment newInstance(Context mContext, FragmentManager fm, String msg) {
        LoadingDialogFragment fragment = newInstance(mContext, fm);
        Bundle bundle = new Bundle();
        bundle.putString(PARAM_SHOW_MSG, msg);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static LoadingDialogFragment newInstance(Context mContext, FragmentManager fm) {
        String tag = LoadingDialogFragment.class.getName();
        Fragment fragment = fm.findFragmentByTag(tag);
        if (fragment == null) {
            fragment = Fragment.instantiate(mContext, tag);
            LoadingDialogFragment dialogFragment = (LoadingDialogFragment) fragment;
            return dialogFragment;
        } else {
            return (LoadingDialogFragment) fragment;
        }
    }

    @Override
    protected boolean isFullScreen() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.common_lodding_dialog;
    }


    @Override
    public void bindUI() {
        initAnimation();
       mIvLoading = view.findViewById(R.id.iv_loading);
        mTvDesc = view.findViewById(R.id.iv_loading);
        timer = new Timer();
        if (timerTask != null && timer != null) {
            timer.schedule(timerTask, 10 * 1000, 60 * 1000);
        }
        statTime = System.currentTimeMillis();
        getDialog().setCanceledOnTouchOutside(false);
        setStyle(R.style.common_load_dialog, R.style.Theme_AppCompat);
        if (getArguments() != null) {
            msg = getArguments().getString(PARAM_SHOW_MSG);
        } else {
            msg = "加载中...";
        }
        mIvLoading.startAnimation(animation);
    }

    private void initAnimation() {
        animation = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        animation.setInterpolator(lin);
        animation.setStartTime(0);
        animation.setDuration(800);//设置动画持续时间
        animation.setRepeatCount(-1);//设置重复次数
        animation.setFillAfter(false);//动画执行完后是否停留在执行完的状态
        animation.setStartOffset(10);//执行前的等待时间
        mIvLoading.setAnimation(animation);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (animation != null) {
            animation.cancel();
            animation = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDismiss(dialog);
    }


    @Override
    public void bindEvent() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
